//
//  HDIdentificationSubView.swift
//  HuaDaoPeifu
//
//  Created by Erickson on 16/4/13.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit

class HDIdentificationSubView: UIView {

    var selectBtned = HDIdButton()
    var currentId = "0"
    init() {
        super.init(frame: CGRect.zero)
        setupViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
    func setupViews() {
        
        //0-身份证 1-护照 2-军官证 3-异常身份证 4-户口本 7-出生证 8-其它 9-工号 11-回乡证 8-驾驶证 8-港澳通行证 8-台湾通行证
        
        let btnW = AppConstant.screenWidth / 3
        let btnH : CGFloat = 39
        let titles:[String] = ["身份证","护照","出生证","回乡证","驾驶证","工号","港澳通行证","户口本","军官证","台湾通行证"]
        for index in 0..<titles.count {
            
            let idBtn = HDIdButton()
            idBtn.setTitle(titles[index], for: UIControlState())
            idBtn.addTarget(self, action: #selector(HDIdentificationSubView.selectIdType(_:)), for: .touchUpInside)
            self.addSubview(idBtn)
            idBtn.tag = index
            let btnX = CGFloat(index % 3) * btnW
            let btnY = CGFloat(index / 3) * btnH
            if index % 3 == 0 {
                idBtn.snp.makeConstraints({ (make) in
                    make.left.equalTo(btnX)
                    make.top.equalTo(btnY)
                    
                })
            }
            if index % 3 == 1 {
                idBtn.snp.makeConstraints({ (make) in
                    make.centerX.equalTo(self)
                    make.top.equalTo(btnY)
                })
            }
            if index % 3 == 2 {
                idBtn.snp.makeConstraints({ (make) in
                    make.left.equalTo(btnX)
                    make.top.equalTo(btnY)
                    
                })
            }
            idBtn.snp.makeConstraints({ (make) in
                make.width.equalTo(btnW)
                make.height.equalTo(btnH)
            })
           
            if index == 0 {
                selectIdType(idBtn)
            }
        }
    }
    
    func selectIdType(_ sender:HDIdButton) {
        currentId = String(sender.tag)
        selectBtned.isSelected = false
        sender.isSelected = true
        selectBtned = sender
        
    }

}

class HDIdButton: UIButton {
    
    init() {
        super.init(frame: CGRect.zero)
        setupViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupViews() {
        self.setImage(UIImage(named: "未选中"), for: UIControlState())
        self.setImage(UIImage(named: "选中"), for: .selected)
        self.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        self.setTitleColor(CenterColor.registerIdTypeColor, for: UIControlState())
        
    }
    
    override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
        return CGRect(x: 15+13+10, y: contentRect.origin.y, width: contentRect.size.width, height: contentRect.size.height)
    }
    
    override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
        return CGRect(x: 10, y: (contentRect.size.height - self.currentImage!.size.width)/2, width: self.currentImage!.size.width, height: self.currentImage!.size.width)
    }
    
}
